package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.blur
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                ArtworkGallery()
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ArtworkGallery() {
    data class Artwork(val imageRes: Int, val title: String, val year: Int)

    val list = listOf(
        Artwork(R.drawable.oip_c__1_, "萧炎融合五种异火", 2022),
        Artwork(R.drawable.oip_c__11_, "三上云岚宗", 2023),
        Artwork(R.drawable.oip_c, "处置不孝师兄", 2024),
        Artwork(R.drawable.oip_c__2_, "判决叛徒", 2025)
    )
    Image(
        painter = painterResource(id = R.drawable.oip_c__11_),
        contentDescription = "背景图片", // 更好的可访问性
        contentScale = ContentScale.Crop,
        modifier = Modifier
            .fillMaxSize()
            .alpha(0.8f) // 可选：添加透明度让内容更易读
            .blur(radius = 4.dp) // 可选：轻微模糊效果
    )
    var index by rememberSaveable { mutableStateOf(0) }
    val current = list[index]

    val snackbarHostState = remember { SnackbarHostState() }
    val scope = rememberCoroutineScope()

    Scaffold(
        snackbarHost = { SnackbarHost(snackbarHostState) },
        modifier = Modifier.fillMaxSize()
    ) { inner ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(inner)
                .padding(horizontal = 18.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            /* 图片 */
            Image(
                painter = painterResource(current.imageRes),
                contentDescription = "artwork",
                modifier = Modifier
                    .width(350.dp)
                    .height(600.dp)
                    .padding(top = 20.dp)
            )
            Text(
                text = "Dou Po Cang Qiong Collection",
                style = MaterialTheme.typography.headlineMedium.copy(
                    fontSize = 24.sp
                ),
                modifier = Modifier.padding(top = 20.dp, bottom = 10.dp)
            )
            Text(
                text = "${current.title} (${current.year})",
                style = MaterialTheme.typography.bodyLarge,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(top=10.dp, bottom = 15.dp)
            )
            Row(
                horizontalArrangement = Arrangement.SpaceEvenly,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(bottom = 24.dp)
            ) {
                Button(onClick = { if (index > 0) index-- }) {
                    Text("Previous")
                }
                Button(onClick = { if (index < list.size - 1) index++ }) {
                    Text("Next")
                }
            }
        }
    }
}
@Preview(showBackground = true)
@Composable
private fun PreviewGallery() {
    MyApplicationTheme { ArtworkGallery() }
}